package com.example.environment.service.impl;

import com.example.environment.entity.base.BaseRspPageBean;
import com.example.environment.enums.UserEnum;
import com.example.environment.repository.UserMapper;
import com.example.environment.service.UserService;
import com.example.environment.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


/**
 *  @author: wangbo(1141294273@qq.com)
 *  @Date: 2021/1/27 19:00
 *  @Description: 业务实现接口父类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public boolean insertSelective(User user) {
        return userMapper.insertSelective(user) > 0;
    }


    @Override
    public boolean updateByUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public User getByExample(String uid) {
        Example example = new Example(User.class);  //查询条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", UserEnum.SUCCESS.getCode());
        criteria.andEqualTo("uid", uid);
        return userMapper.selectOneByExample(example);
    }

    @Override
    public BaseRspPageBean<User> pageQueryUser(User user, int pageNum, int size) {
        PageHelper.startPage(pageNum,size);
        PageInfo pageInfo =  new PageInfo(userMapper.select(user));
        BaseRspPageBean<User> baseRspPageBean = new BaseRspPageBean<>();
        baseRspPageBean.setTotal(pageInfo.getTotal());
        baseRspPageBean.setPage(pageInfo.getPrePage());
        baseRspPageBean.setData(pageInfo.getList());
        return baseRspPageBean;

    }

    @Override
    public User login(User user) {
        Example example = new Example(User.class);  //查询条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", UserEnum.SUCCESS.getCode());
        criteria.andEqualTo("password", user.getPassword());
        criteria.andEqualTo("name", user.getName());
        return userMapper.selectOneByExample(example);
    }

}
